/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.integration;

import java.io.IOException;
import java.util.Map;

import nu.zoom.android.gdata.project.client.WKC;
import android.net.Uri;
import android.util.Log;

import com.google.api.client.apache.ApacheHttpTransport;
import com.google.api.client.googleapis.GoogleTransport;
import com.google.api.client.googleapis.GoogleUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.xml.XmlNamespaceDictionary;
import com.google.api.client.xml.atom.AtomParser;

/**
 * @author Johan Maasing
 * 
 */
public class ProjectHostingNetClient implements ProjectHostingClient {
	private static final String LOG_TAG = WKC.LOG_TAG;

	private static final XmlNamespaceDictionary NAMESPACE_DICTIONARY = new XmlNamespaceDictionary();
	static {
		Map<String, String> map = NAMESPACE_DICTIONARY.namespaceAliasToUriMap;
		map.put("", "http://www.w3.org/2005/Atom");
		map.put("atom", "http://www.w3.org/2005/Atom");
		map.put("exif", "http://schemas.google.com/photos/exif/2007");
		map.put("gd", "http://schemas.google.com/g/2005");
		map.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
		map.put("georss", "http://www.georss.org/georss");
		map.put("gml", "http://www.opengis.net/gml");
		map.put("gphoto", "http://schemas.google.com/photos/2007");
		map.put("media", "http://search.yahoo.com/mrss/");
		map.put("openSearch", "http://a9.com/-/spec/opensearch/1.1/");
		map.put("xml", "http://www.w3.org/XML/1998/namespace");
		map.put("issues",
				"http://schemas.google.com/projecthosting/issues/2009");
	}

	public IssueOverviewFeed getIssues(final Uri uri, final int startPosition, final int batchSize, final String cannedQuery)
			throws IOException {
		Log.d(LOG_TAG, "Getting issues for : " + uri);

		HttpRequest request = buildRequest();

		GoogleUrl url = new GoogleUrl(uri.toString());
		url.put("fields", "link,openSearch:totalResults,entry(title,id)");
		url.put("start-index", startPosition);
		url.put("max-results", batchSize);
		if (cannedQuery != null) {
			url.put("can", cannedQuery) ;
		}
		request.setUrl(url.build());

		HttpResponse httpResponse = request.execute();

		return httpResponse.parseAs(IssueOverviewFeed.class);
	}

	@Override
	public IssueDetail getIssue(final Uri uri) throws IOException {
		HttpRequest request = buildRequest();
		GoogleUrl url = new GoogleUrl(uri.toString());
		request.setUrl(url.build());
		HttpResponse httpResponse = request.execute();
		return httpResponse.parseAs(IssueDetail.class);
	}

	/**
	 * @return
	 */
	private HttpRequest buildRequest() {
		HttpTransport.setLowLevelHttpTransport(ApacheHttpTransport.INSTANCE);
		HttpTransport transport = GoogleTransport.create();
		AtomParser parser = new AtomParser();
		parser.namespaceDictionary = NAMESPACE_DICTIONARY;
		transport.addParser(parser);

		HttpRequest request = transport.buildGetRequest();
		return request;
	}

	/* (non-Javadoc)
	 * @see nu.zoom.android.gdata.project.integration.ProjectHostingClient#getComments(android.net.Uri, int)
	 */
	@Override
	public CommentFeed getComments(Uri uri, int startPosition, final int batchSize)
			throws IOException {
		Log.d(LOG_TAG, "Getting issues for : " + uri);

		HttpRequest request = buildRequest();

		GoogleUrl url = new GoogleUrl(uri.toString());
		url.put("fields", "link,openSearch:totalResults,entry(title,content,published)");
		url.put("start-index", startPosition);
		url.put("max-results", batchSize);
		request.setUrl(url.build());

		HttpResponse httpResponse = request.execute();

		return httpResponse.parseAs(CommentFeed.class);
	}
}
